package com.nwpu.coursesupport.service.imple;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.StudentSearchCriteria;
import com.nwpu.coursesupport.entity.Student;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.mapper.StudentMapper;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.RegisterParam;
import com.nwpu.coursesupport.service.param.StudentServiceParam;
import com.nwpu.coursesupport.tool.EncryptionUtil;
import com.nwpu.coursesupport.tool.StringUtil;
import com.nwpu.coursesupport.tool.UpdateUtil;
/**
 * @author archerkang
 */
@Service
public class StudentService extends UserService{
	
    @Autowired
    private StudentMapper studentMapper;
    
    protected static Logger logger = LoggerFactory.getLogger(StudentService.class);


    public JSONResult detail(IDParam p){
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Student student = this.studentMapper.findById(p.getId());
        if(student==null){
        	logger.error("该用户不存在");
        	return rst.fail("该用户不存在");
        }
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, student);
        dao.setUserCode(student.getStudentCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }

    public JSONResult create(RegisterParam p){
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        StudentSearchCriteria sc=new StudentSearchCriteria();
        sc.setStudentCode(p.getUserCode());
        List<Student> list=this.studentMapper.find(sc);
        if(list!=null&&list.size()>0){
        	logger.error("该用户已存在");
        	return rst.fail("该用户已存在");
        }
        Student student = new Student();
        UpdateUtil.setValues(student, p);
		student.setStudentCode(p.getUserCode());
        student.setPassword(EncryptionUtil.Encryption(student.getPassword(),student.getStudentCode()));
        if(StringUtil.isEmpty(student.getPicture())){
        	student.setPicture(getHeaderImage(student.getGender()));
        }
        Date date=new Date();
        student.setCreateDate(date);
        student.setUpdateDate(date);
        if (!this.studentMapper.save(student)){
        	logger.error("保存失败");
            return rst.fail("保存失败");
        }
        rst.addData("student", student);
        rst.success();
        return rst;
    }

    public JSONResult updateAvatar(RegisterParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ||p.getPicture()==null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        Student student = this.studentMapper.findById(id);
        if (student == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }
        student.setPicture(p.getPicture());
        if(!this.studentMapper.update(student)){
        	logger.error("用户修改失败");
        	return rst.fail("用户修改失败");
        }
        getSession().setAttribute("image",student.getPicture());
        return rst.success();
    }
    
    public JSONResult update(RegisterParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        p.setId(id);
        Student student = this.studentMapper.findById(p.getId());
        if (student == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }
        if (UpdateUtil.setValues(student, p) > 0){
        	
        	student.setUpdateDate(new Date());
            if (!this.studentMapper.update(student)){
            	logger.error("保存失败");
                return rst.fail("保存失败");
            }
        }
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, student);
        dao.setUserCode(student.getStudentCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }

    public JSONResult changePass(StudentServiceParam.ChangePassParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if(p==null){
        	logger.error("参数有误");
        	return rst.fail("参数有误");
        }
        p.setId((Integer)getSession().getAttribute("id"));
        p.setStudentCode((String)getSession().getAttribute("studentCode"));
        Student student=this.studentMapper.findByStudentCode(p.getStudentCode());
        if(!student.getPassword().equals(p.getOldPassword())){
        	logger.error("旧密码错误");
        	return rst.fail("旧密码错误");
        }
        p.setUpdateDate(new Date());
        if(!this.studentMapper.changePass(p)){
        	logger.error("密码修改失败");
        	return rst.fail("密码修改失败");
        }

        rst.success();
        return rst;
        
    }
    
    public JSONResult listData(StudentSearchCriteria sc) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if (sc == null || StringUtil.isEmpty(sc.getKeywords())){
            return rst.fail("参数有误");
        }
        
        List<Student> list = this.studentMapper.find(sc);
        if (list == null){
        	logger.error("数据查询失败");
            return rst.fail("数据查询失败");
        }
        
        rst.addData("list", list);
        rst.addData("search", sc);
        rst.success();
        return rst;
    }
    
    
    public Student findByStudentCode(String studentCode){
    	Student student = this.studentMapper.findByStudentCode(studentCode);
    	return student;
    }
    
    
    /**
     * 初版忘记密码这儿简单处理
     * 用户验证其注册所留Email
     * 	若通过，允许修改
     * 	否则，不允许修改
     * @param p
     * @return
     *//*
    public JSONResult forgetPass(ChangePassParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if(p==null||p.getStudentCode() == null){
        	return rst.fail("参数有误");
        }
        
        Student student=this.studentMapper.findByStudentCode(p.getStudentCode());
        if(student == null){
        	return rst.fail("该用户不存在");
        }
        
        if(student.getEmail().equals(p.getEmail())){
        	return rst.fail("你所输入的email与注册所留不同");
        }
        
        p.setUpdateDate(new Date());
        p.setId(student.getId());
        if(!this.studentMapper.changePass(p)){
        	return rst.fail("密码修改失败");
        }

        rst.success();
        return rst;
    }*/
   
}
